{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plotter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Author:** [Leah Nguyen](https://github.com/nduongthucanh)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Description"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An automation program to plot data with different visualisations by user selections"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Requirement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install chart_studio\n",
    "!pip install cufflinks"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import chart_studio.plotly as pl\n",
    "import plotly.offline as po\n",
    "import cufflinks as cf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting cufflinks to offline mode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "po.init_notebook_mode(connected = True)\n",
    "cf.go_offline()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Define automation functions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " Differentcreate data options using function ```createdata```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def createdata(data):\n",
    "    if(data == 1):\n",
    "        x = np.random.rand(100, 5)\n",
    "        df1 = pd.DataFrame(x, columns = ['A', 'B', 'C', 'D', 'E'])\n",
    "    elif(data == 2):\n",
    "        x = [0,0,0,0,0]\n",
    "        r1 = [0,0,0,0,0]\n",
    "        r2 = [0,0,0,0,0]\n",
    "        r3 = [0,0,0,0,0]\n",
    "        r4 = [0,0,0,0,0]\n",
    "        print('Enter the values for columns ')\n",
    "        i = 0\n",
    "        for i in (0,1,2,3,4):\n",
    "            x[i] = input()\n",
    "            i = i + 1\n",
    "        print('Enter the values for first row ')\n",
    "        i = 0\n",
    "        for i in (0,1,2,3,4):\n",
    "            r1[i] = int(input())\n",
    "            i = i + 1\n",
    "        print('Enter the values for second row ')\n",
    "        i = 0\n",
    "        for i in (0,1,2,3,4):\n",
    "            r2[i] = int(input())\n",
    "            i = i + 1\n",
    "        print('Enter the values for third row ')\n",
    "        i = 0\n",
    "        for i in (0,1,2,3,4):\n",
    "            r3[i] = int(input())\n",
    "            i = i + 1\n",
    "        print('Enter the values for forth row ')\n",
    "        i = 0\n",
    "        for i in (0,1,2,3,4):\n",
    "            r4[i] = int(input())\n",
    "            i = i + 1\n",
    "        df1= pd.DataFrame([r1, r2, r3, r4], columns = x)\n",
    "    elif(data == 3):\n",
    "        file = input('Enter the file name ')\n",
    "        x = pd.read_csv(file)\n",
    "        df1 = pd.DataFrame(x)\n",
    "    else:\n",
    "        print('Dataframe creation failed, please enter in between 1 to 3 and try ')\n",
    "    return df1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plotting all columns option using function ```plotter```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plotter(plot):\n",
    "    if(plot == 1):\n",
    "        finalplot = df1.iplot(kind = 'scatter')\n",
    "    elif(plot == 2):\n",
    "        finalplot = df1.iplotfinalplot = df1.iplot(kind = 'scatter', mode = 'markers', symbol = 'x', colorscale = 'paired')\n",
    "    elif(plot == 3):\n",
    "        finalplot = df1.iplot(kind = 'bar')\n",
    "    elif(plot == 4):\n",
    "        finalplot = df1.iplot(kind = 'hist')\n",
    "    elif(plot == 5):\n",
    "        finalplot = df1.iplot(kind = 'box')\n",
    "    elif(plot == 6):\n",
    "        finalplot = df1.iplot(kind = 'surface')\n",
    "    else:\n",
    "        finalplot = print('Select only between 1 to 7')\n",
    "    return finalplot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Plotting by selecting specific columns using function ```plotter2```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def plotter2(plot):\n",
    "    col = input('Enter the number of columns you want to plot by selecting only 1, 2 or 3')\n",
    "    col = int(col)\n",
    "    if(col == 1):\n",
    "        coln = input('Enter the column you want to plot by selecting any column from dataframe head')\n",
    "        if(plot == 1):\n",
    "            finalplot = df1[coln].iplot(kind = 'scatter')\n",
    "        elif(plot == 2):\n",
    "            finalplot = df1[coln].iplot(kind = 'scatter', mode = 'markers', symbol = 'x', colorscale = 'paired')\n",
    "        elif(plot == 3):\n",
    "            finalplot = df1[coln].iplot(kind = 'bar')\n",
    "        elif(plot == 4):\n",
    "            finalplot = df1[coln].iplot(kind = 'hist')\n",
    "        elif(plot == 5):\n",
    "            finalplot = df1[coln].iplot(kind = 'box')\n",
    "        elif(plot == 6 or plot == 7):\n",
    "            finalplot = print(\"Bubble plot and surface plot require more than one column arguements\")\n",
    "        else:\n",
    "            finalplot = print('Select only between 1 to 7')               \n",
    "    elif(col == 2):\n",
    "        print('Enter the columns you want to plot by selecting from dataframe head')\n",
    "        x = input('First column')\n",
    "        y = input('Second column')\n",
    "        if(plot == 1):\n",
    "            finalplot = df1[[x, y]].iplot(kind = 'scatter')\n",
    "        elif(plot == 2):\n",
    "            finalplot = df1[[x, y]].iplot(kind = 'scatter', mode = 'markers', symbol = 'x', colorscale = 'paired')\n",
    "        elif(plot == 3):\n",
    "            finalplot = df1[[x, y]].iplot(kind = 'bar')\n",
    "        elif(plot == 4):\n",
    "            finalplot = df1[[x, y]].iplot(kind = 'hist')\n",
    "        elif(plot == 5):\n",
    "            finalplot = df1[[x, y]].iplot(kind = 'box')\n",
    "        elif(plot == 6):\n",
    "            finalplot = df1[[x, y]].iplot(kind = 'surface')\n",
    "        elif(plot == 7):\n",
    "            size = input('Please enter the size column for bubble plot')\n",
    "            finalplot = df1.iplot(kind = 'bubble', x=x, y=y, size=size)\n",
    "        else:\n",
    "            finalplot = print('Select only between 1 to 7')              \n",
    "    elif(col == 3):\n",
    "        print('Enter the columns you want to plot')\n",
    "        x = input('First column')\n",
    "        y = input('Second column')\n",
    "        z = input('Third column')\n",
    "        if(plot == 1):\n",
    "            finalplot = df1[[x, y, z]].iplot(kind = 'scatter')\n",
    "        elif(plot == 2):\n",
    "            finalplot = df1[[x, y, z]].iplot(kind = 'scatter', mode = 'markers', symbol = 'x', colorscale = 'paired')\n",
    "        elif(plot == 3):\n",
    "            finalplot = df1[[x, y, z]].iplot(kind = 'bar')\n",
    "        elif(plot == 4):\n",
    "            finalplot = df1[[x, y, z]].iplot(kind = 'hist')\n",
    "        elif(plot == 5):\n",
    "            finalplot = df1[[x, y, z]].iplot(kind = 'box')\n",
    "        elif(plot == 6):\n",
    "            finalplot = df1[[x, y, z]].iplot(kind = 'surface')\n",
    "        elif(plot == 7):\n",
    "            size = input('Please enter the size column for bubble plot')\n",
    "            finalplot = df1.iplot(kind = 'bubble', x=x, y=y, z=z, size=size)\n",
    "        else:\n",
    "            finalplot = print('Select only between 1 to 7')\n",
    "    else:\n",
    "        finalplot = print('Please enter only 1, 2 or 3 and try again')\n",
    "    return finalplot"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "User selection with plotting table using function ```main```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def main(cat):\n",
    "    if(cat == 1):\n",
    "        print('Select the type of plot you want to plot by writing 1 to 6')\n",
    "        print('1. Line Plot')\n",
    "        print('2. Scatter Plot')\n",
    "        print('3. Bar Plot')\n",
    "        print('4. Histogram')\n",
    "        print('5. Box Plot')\n",
    "        print('6. Surface Plot')\n",
    "        plot = int(input())\n",
    "        output = plotter(plot)\n",
    "    elif(cat == 2):\n",
    "        print('Select the type of plot you want to plot by writing 1 to 7')\n",
    "        print('1. Line Plot')\n",
    "        print('2. Scatter Plot')\n",
    "        print('3. Bar Plot')\n",
    "        print('4. Histogram')\n",
    "        print('5. Box Plot')\n",
    "        print('6. Surface Plot')\n",
    "        print('7. Bubble Plot')\n",
    "        plot = int(input())\n",
    "        output = plotter2(plot)\n",
    "    else:\n",
    "        print('Please enter 1 or 2 and try again')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Scripts for code executions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. User choose the way they want data to be create:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "print('Select the type of data you need to plot(By writing 1, 2, or 3)')\n",
    "print('1. Random data with 100 rows and 5 columns')\n",
    "print('2. Customize dataframe with 5 colums and 4 rows')\n",
    "print('3. Upload csv/json/txt file')\n",
    "data = int(input())\n",
    "df1 = createdata(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Inspecting the first 5 rows of the dataframe using ```head()``` function:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('Your DataFrame head is given below, check the columns to plot using cufflinks')\n",
    "df1.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Selecting the number of columns they want to plot (whole table or selected columns only):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print('What kind if plot you need, the complete data plot or columns plot')\n",
    "cat = input('Press 1 for plotting all columns or press 2 for specifying columns to plot')\n",
    "cat = int(cat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. Choosing the plot types:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "main(cat)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
